//
//  SQWelcomeView.swift
//  SwiftWeibo
//
//  Created by 苏强 on 16/12/2.
//  Copyright © 2016年 suqianghotel. All rights reserved.
//

import UIKit
import SDWebImage

class SQWelcomeView: UIView {

    @IBOutlet weak var avatarBottomConstrait: NSLayoutConstraint!
    
    @IBOutlet weak var avatar: UIImageView!
    
    @IBOutlet weak var titleLabel: UILabel!
    
    class func welcomeView()->SQWelcomeView
    {
        let nib = UINib(nibName: "SQWelcomeView", bundle: nil)
        
        let view = nib.instantiate(withOwner: nil, options: nil)[0] as! SQWelcomeView
        
        view.frame = UIScreen.main.bounds
        view.avatar.layer.masksToBounds = true
        view.avatar.layer.cornerRadius = 85 * 0.5;
        view.titleLabel.alpha = 0;
        view.avatar.alpha = 0
        view.titleLabel.textColor = UIColor(hex:0x757575)

        return view
    }

    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.avatar.sd_setImage(with: URL(string:(SQUserAccountManager.shared.userInfo?.avatar_large)!), placeholderImage: UIImage(named:"avatar_default_big"))
        
        let name = SQUserAccountManager.shared.userInfo?.screen_name ?? ""
        
        self.titleLabel.text = "欢迎\(name)回来"
        
    }
    
    override func didMoveToWindow() {
        super.didMoveToWindow()
        
        self.layoutIfNeeded()
        
        UIView.animate(withDuration: 2.5, delay: 0, usingSpringWithDamping: 8, initialSpringVelocity: 10, options: [], animations: { () in
            self.avatarBottomConstrait.constant = UIScreen.main.bounds.height - 250
            self.titleLabel.alpha = 1
            self.avatar.alpha = 1
            self.layoutIfNeeded()
        }){ _ in
            UIView.animate(withDuration: 0.3, delay: 1, animations: {() in
                self.alpha = 0;
            }, completion: {(_) in
                self.removeFromSuperview()
            })
        }
        
    }

}
